// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueSignaturePad from 'vue-signature-pad';
import App from './App'
import myFilters from '../test/unit/myFilters'
import router from './router'
import WeVue from 'we-vue'
import VueWechatTitle from 'vue-wechat-title'
import 'we-vue/lib/style.css'
import 'swiper/dist/css/swiper.css';
import '../static/css/reset.css'
import global from '../test/unit/global'
import myUtils from '../test/unit/myUtils'
import {getStore, setStore} from '../test/unit/storage'
import {IndexBar, IndexAnchor, Tab, Tabs, Cell,Dialog} from 'vant';
import 'vant/lib/index.css';
import updateVersion from '../test/unit/updateVersion'

Vue.config.productionTip = false;
Vue.prototype.$global = global;
Vue.prototype.$myUtils = myUtils;

for (let key in myFilters) {  //一定要放在 new Vue之前 注册全局过滤器
  Vue.filter(key, myFilters[key])
}
Vue.use(WeVue);
Vue.use(VueSignaturePad);
Vue.use(VueWechatTitle);
Vue.use(IndexBar).use(IndexAnchor).use(Tab).use(Tabs).use(Cell).use(Dialog);


router.beforeEach((to, from, next) => {
  //判断当前代码版本是否与服务器中代码版本一致，如不一致则刷新页面获取最新
  updateVersion.isNewVersion();
  let token = getStore("token");
  if (myUtils.isNull(token)) {
    if (to.meta.isAuth) {
      next({path: '/'})
    } else {
      next();
    }
  } else {
    if (from.name == 'login' || from.name == 'register') {
      let query = from.query;
      let result = query.hasOwnProperty('backUrl');
      if (result) {
        let backUrl = query.backUrl;
        delete query.backUrl;
        next({path: '/' + backUrl, query: query})
      } else {
        next();
      }
    } else {
      next();
    }
  }
});

new Vue({
  el: '#app',
  router,
  // components: { App },
  // template: '<App/>',
  render: h => h(App)
});
